package com.hs.egova.tool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.Collator;
import java.util.Collection;
import java.util.Locale;
import java.util.Random;
import java.util.regex.Pattern;

/**
 * 字符串工具
 * @author husheng
 * @version v1.0
 * @date 2021/7/27 11:14
 * @email husheng@egova.com.cn
 */
public class StringUtils {
    public static String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789";
    private static final Pattern PATTERN_INTEGER = Pattern.compile("-?\\d+");

    public StringUtils() {
    }

    public static String getRandomStr(Integer length) {
        Random rand = new Random();
        StringBuffer str = new StringBuffer();
        if (length == null) {
            length = rand.nextInt(100);
        } else if (length < 1) {
            length = Math.abs(length);
            if (length == 0) {
                length = 1;
            }
        }

        for(int i = 0; i < length; ++i) {
            int start = rand.nextInt(base.length());
            str.append(base.substring(start, start + 1));
        }

        return str.toString();
    }

    public static boolean isEmpty(String str) {
        if (str == null) {
            return true;
        } else {
            return str.trim().isEmpty();
        }
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String nullToStr(String str) {
        return str == null ? "" : str;
    }

    public static String nullToStr(Object object) {
        return object == null ? "" : String.valueOf(object);
    }

    public static String getContent(String strUrl) throws UnsupportedEncodingException, IOException {
        URL url = new URL(strUrl);
        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
        String s = "";
        StringBuffer sb = new StringBuffer("");

        while((s = br.readLine()) != null) {
            sb.append(s + "\r\n");
        }

        br.close();
        return sb.toString();
    }

    public static String getFileExt(String fileName) {
        if (fileName != null) {
            int i = fileName.lastIndexOf(46);
            if (i != -1) {
                return fileName.substring(i + 1);
            }
        }

        return "";
    }

    public static String concat(Object... params) {
        StringBuilder sb = new StringBuilder("");
        if (null == params) {
            return sb.toString();
        } else {
            Object[] var2 = params;
            int var3 = params.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                Object param = var2[var4];
                sb.append(param);
            }

            return sb.toString();
        }
    }

    public static String replaceQuotes(String str) {
        return str == null ? "" : str.replaceAll("'", "''");
    }

    public static String firstToUpperCase(String str) {
        return str != null && str.length() > 0 ? str.replaceFirst(str.substring(0, 1), str.substring(0, 1).toUpperCase()) : null;
    }

    public static String joinString(Object[] array, String separator) {
        return org.apache.commons.lang.StringUtils.join(array, separator);
    }

    public static String joinString(Collection collection, String separator) {
        return org.apache.commons.lang.StringUtils.join(collection, separator);
    }

    public static int compareChineseString(String sLeft, String sRight) {
        String sL = sLeft.toLowerCase(Locale.CHINESE);
        String sR = sRight.toLowerCase(Locale.CHINESE);
        int len1 = sL.length();
        int len2 = sR.length();
        int i1 = 0;

        for(int i2 = 0; i1 < len1 && i2 < len2; ++i2) {
            char ch1 = sL.charAt(i1);
            char ch2 = sR.charAt(i2);
            if (ch1 != ch2) {
                if (getCharType(ch1) != getCharType(ch2)) {
                    return getCharType(ch1) - getCharType(ch2);
                }

                switch(getCharType(ch1)) {
                    case 1:
                    case 3:
                        return Character.toUpperCase(ch1) - Character.toUpperCase(ch2);
                    case 2:
                        int num1EndIdx = getNumEndIndex(sL, i1);
                        int num2EndIdx = getNumEndIndex(sR, i2);
                        int num1 = Integer.parseInt(sL.substring(i1, num1EndIdx + 1));
                        int num2 = Integer.parseInt(sR.substring(i2, num2EndIdx + 1));
                        if (num1 != num2) {
                            return num1 - num2;
                        }

                        i1 = num1EndIdx;
                        i2 = num2EndIdx;
                        break;
                    case 4:
                        return Collator.getInstance(Locale.CHINESE).compare(String.valueOf(ch1), String.valueOf(ch2));
                }
            }

            ++i1;
        }

        return len1 != len2 ? len1 - len2 : 0;
    }

    private static int getCharType(char ch) {
        if (ch >= 19968 && ch <= '龥') {
            return 4;
        } else if (Character.isDigit(ch)) {
            return 2;
        } else {
            return Character.isLetter(ch) ? 3 : 1;
        }
    }

    private static int getNumEndIndex(String s, int beginIndex) {
        int i;
        for(i = beginIndex; i < s.length() && getCharType(s.charAt(i)) == 2; ++i) {
        }

        return i - 1;
    }

    public static boolean isInteger(String str) {
        return isEmpty(str) ? false : PATTERN_INTEGER.matcher(str).matches();
    }

    public static boolean isEmptyArray(String str) {
        return isEmpty(str) || "[]".equals(str);
    }

    public static String utf8Convert(String str) {
        if (isEmpty(str)) {
            return str;
        } else {
            return str.equals(new String(str.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1)) ? new String(str.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8) : str;
        }
    }

    public static int getWidth(String str) {
        try {
            return (new String(str.getBytes("GB2312"), StandardCharsets.ISO_8859_1)).length();
        } catch (Exception var2) {
            return 0;
        }
    }

    public static boolean isNumeric(String str) {
        try {
            new BigDecimal(str);
            return true;
        } catch (Exception var2) {
            return false;
        }
    }
}
